package com.markovich.projects.netcoverage;

import java.util.ArrayList;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.markovich.projects.netcoverage.services.ConnectionService;
import com.markovich.projects.netcoverage.services.TrackingService;

public class MainActivity extends ActionBarActivity implements LocationListener {

	private LocationManager locationManager;
	private static final long MIN_TIME = 1000;
	private static final float MIN_DISTANCE = 25;
	private ArrayList<Marker> markerList;
	private TrackingService trackingService;

	GoogleMap map;

	TelephonyManager telephonyManager;
	PhoneStateListener listener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
				.getMap();
		
		this.markerList = new ArrayList<Marker>();
		this.trackingService = new TrackingService();

		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				MIN_TIME, MIN_DISTANCE, this);

		telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

		listener = new ConnectionService(this);

		telephonyManager.listen(listener,
				PhoneStateListener.LISTEN_SERVICE_STATE);
		
		telephonyManager.listen(listener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void removeAllMarkerFromMap() {
		for (Marker m : this.markerList) {
			m.remove();
		}
	}

	public void drawToast(CharSequence text, int duration) {

		Context context = getApplicationContext();

		Toast toast = Toast.makeText(context, text, duration);
		toast.show();

	}

	public void onLocationChanged(Location location) {

		this.removeAllMarkerFromMap();

		this.drawToast("onLocationChagnge", Toast.LENGTH_SHORT);

		LatLng latLng = new LatLng(location.getLatitude(),
				location.getLongitude());

		Log.i("onLocationChanged", "Changed Location to " + "GPS: \n Lon: "
				+ latLng.longitude + " Lat: " + latLng.latitude);

		this.drawMarker(latLng);
		this.drawCircle(latLng);

		map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16));

	}

	public void drawMarker(LatLng latLng) {
		Marker positionMarker = map.addMarker(new MarkerOptions().position(
				latLng).flat(true));
		this.markerList.add(positionMarker);
	}

	public void drawCircle(LatLng latLng) {

		CircleOptions circleOptions = new CircleOptions().center(latLng)
				.radius(100); // In meters
		circleOptions.strokeWidth(0);
		
		if (ConnectionService.serviceAvailable == true) {

			
			circleOptions.fillColor(this.trackingService.getColor(ConnectionService.currentSignalStrength));
		} else {
			// 0x + transpareny (2 chars) + 5 chars color
			circleOptions.fillColor(0x50ff0000);
		}

		map.addCircle(circleOptions);
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
	}

}
